function isSupportedRule(selector) {
  return !selector.includes(":hover");
}

module.exports = function (config, root, opts) {
  root.walkRules(postrule => {
    // mp 小程序下

    // 删除所有媒体查询
    if (postrule.parent.name === "media") {
      postrule.parent.remove();
    }

    // 替换选择器 对象从 * div等 为view
    if (postrule.selector.includes(":not(template) ~ :not(template)")) {
      postrule.selector = postrule.selector.replace(
        ":not(template) ~ :not(template)",
        "view + view"
      );
    }
    if (postrule.selector.includes(":not([hidden]) ~ :not([hidden])")) {
      postrule.selector = postrule.selector.replace(
        ":not([hidden]) ~ :not([hidden])",
        "view + view"
      );
    }

    if (postrule.selector.includes("*")) {
      postrule.selector = postrule.selector.replace("*", "view");
    }

    if (postrule.selector.includes("*::before")) {
      postrule.selector = postrule.selector.replace(
        "*::before",
        "view::before"
      );
    }

    if (postrule.selector.includes("*::after")) {
      postrule.selector = postrule.selector.replace("*::after", "view:after");
    }

    if (!isSupportedRule(postrule.selector)) {
      postrule.remove();
    }

    postrule.walkDecls(decl => {
      if (decl.prop === "visibility") {
        switch (decl.value) {
          case "hidden":
            decl.replaceWith(decl.clone({ value: "collapse" }));
            return;
        }
      }

      if (decl.prop === "vertical-align") {
        switch (decl.value) {
          case "middle":
            decl.replaceWith(decl.clone({ value: "center" }));
            return;
        }
      }

      // if (!isSupportedProperty(decl.prop, decl.value)) {
      //   rule.removeChild(decl);

      //   if (rule.nodes.length === 0) {
      //     rule.remove();
      //   }
      // }
    });
  });
};
